package com.yanqu.road.dao.impl.activity.ppl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ppl.PplLine;
import com.yanqu.road.entity.activity.ppl.PplUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 泡泡龙用户数据dao
 */
public class PplUserDataDaoImpl extends TempDataObjectDao<PplUserData> {

    public static String getPplUserDataSql = "select * from t_u_activity_ppl where userId=? and activityId=?";

    public PplUserData getPplUserData(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPplUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updatePplUserDataSql = "update t_u_activity_ppl set beautyId=?,headColor=?,curColor=?,curMaxY=?,energy=?,lastRecoveryTime=?,pplLineList=?,drumList=?,score=?,todayScore=?,scoreResetTime=?,headColor4=?,curColor4=?,curMaxY4=?,pplLineList4=?,headColor16=?,curColor16=?,curMaxY16=?,pplLineList16=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(PplUserData pplUserData) {
        pplUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getHeadColor1()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurColor1()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurMaxY1()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, pplUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(pplUserData.getPplLineList1()))));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(pplUserData.getDrumList()))));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getTodayScore()));
        para.put(i++, new DbParameter(Types.BIGINT, pplUserData.getScoreResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getHeadColor4()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurColor4()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurMaxY4()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(pplUserData.getPplLineList4()))));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getHeadColor16()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurColor16()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurMaxY16()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(pplUserData.getPplLineList16()))));
        para.put(i++, new DbParameter(Types.BIGINT, pplUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getActivityId()));
        if (getDb().executeNoneQuery(updatePplUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPplUserDataSql = "insert into t_u_activity_ppl(userId,activityId,beautyId,headColor,curColor,curMaxY,energy,lastRecoveryTime,pplLineList,drumList,score,todayScore,scoreResetTime,headColor4,curColor4,curMaxY4,pplLineList4,headColor16,curColor16,curMaxY16,pplLineList16) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(PplUserData pplUserData) {
        pplUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, pplUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getHeadColor1()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurColor1()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurMaxY1()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, pplUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(pplUserData.getPplLineList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(pplUserData.getDrumList())));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getTodayScore()));
        para.put(i++, new DbParameter(Types.BIGINT, pplUserData.getScoreResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getHeadColor4()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurColor4()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurMaxY4()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(pplUserData.getPplLineList4())));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getHeadColor16()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurColor16()));
        para.put(i++, new DbParameter(Types.INTEGER, pplUserData.getCurMaxY16()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(pplUserData.getPplLineList16())));
        if (getDb().executeNoneQuery(insertPplUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected PplUserData parseData(ResultSet rs) throws SQLException {
        PplUserData pplUserData = new PplUserData();
        pplUserData.setUserId(rs.getLong("userId"));
        pplUserData.setActivityId(rs.getInt("activityId"));
        pplUserData.setBeautyId(rs.getInt("beautyId"));
        pplUserData.setHeadColor1(rs.getInt("headColor"));
        pplUserData.setCurColor1(rs.getInt("curColor"));
        pplUserData.setCurMaxY1(rs.getInt("curMaxY"));
        pplUserData.setEnergy(rs.getInt("energy"));
        pplUserData.setLastRecoveryTime(rs.getLong("lastRecoveryTime"));
        pplUserData.setPplLineList1(JSON.parseObject(rs.getString("pplLineList"), new TypeReference<List<PplLine>>() {
        }));
        pplUserData.setDrumList(JSON.parseObject(rs.getString("drumList"), new TypeReference<List<Integer>>() {
        }));
        pplUserData.setScore(rs.getInt("score"));
        pplUserData.setTodayScore(rs.getInt("todayScore"));
        pplUserData.setScoreResetTime(rs.getLong("scoreResetTime"));
        pplUserData.setHeadColor4(rs.getInt("headColor4"));
        pplUserData.setCurColor4(rs.getInt("curColor4"));
        pplUserData.setCurMaxY4(rs.getInt("curMaxY4"));
        pplUserData.setPplLineList4(JSON.parseObject(rs.getString("pplLineList4"), new TypeReference<List<PplLine>>() {
        }));
        pplUserData.setHeadColor16(rs.getInt("headColor16"));
        pplUserData.setCurColor16(rs.getInt("curColor16"));
        pplUserData.setCurMaxY16(rs.getInt("curMaxY16"));
        pplUserData.setPplLineList16(JSON.parseObject(rs.getString("pplLineList16"), new TypeReference<List<PplLine>>() {
        }));
        return pplUserData;
    }
}
